package org.zgy.stock.controller;

import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zgy.stock.service.*;
import org.zgy.stock.service.holder.StockFixHolderInfoService;
import org.zgy.stock.service.holder.StockFreeHoldersInfoService;
import org.zgy.stock.service.holder.StockHolderNumsInfoService;
import org.zgy.stock.service.holder.StockMainHolderInfoService;
import org.zgy.stock.threads.CompanyInfoThead;
import org.zgy.stock.threads.HoldOrgInfoThread;
import org.zgy.stock.threads.StockIssueInfoThread;
import org.zgy.stock.threads.StockPriceThread;
import org.zgy.stock.threads.holder.StockFixHolerInfoThread;
import org.zgy.stock.threads.holder.StockFreeHoldersInfoThread;
import org.zgy.stock.threads.holder.StockHolderInfoThread;
import org.zgy.stock.threads.holder.StockMainHolderInfoThread;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

@RestController
@RequestMapping(value = "dfcf/api")
public class StockApiController {
    @Autowired
    private DfcfServiceApi dfcfServiceApi;
    @Autowired
    private StockBaseService stockBaseService;
    @Autowired
    private StockPriceService stockPriceService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private StockIssueInfoService stockIssueInfoService;
    @Autowired
    private HoldOrgInfoService holdOrgInfoService;
    @Autowired
    private StockHolderNumsInfoService stockHolderNumsInfoService;
    @Autowired
    private StockFreeHoldersInfoService stockFreeHoldersInfoService;
    @Autowired
    private StockFixHolderInfoService stockFixHolderInfoService;
    @Autowired
    private StockMainHolderInfoService stockMainHolderInfoService;

    @GetMapping(value = "getStockList")
    public void getStockList() {
        val start = Instant.now();
        dfcfServiceApi.getStockList();
        var end = Instant.now();
        System.out.println("the delta is: " + Duration.between(start, end));
    }

    @GetMapping(value = "getStockPrice")
    public void getStockPrice() {
        /*var list = stockBaseService.getList();
        var handle_list = stockPriceService.getHandledCodeList();
        List<String> codeList = new ArrayList<>();
        list.forEach(item -> {
            if (!handle_list.contains(item)) {
                codeList.add(item);
            }
        });
        System.out.println(codeList.size());
        codeList.parallelStream().forEach(code -> {
            StockPriceThread stockPriceThread = new StockPriceThread(code, stockPriceService);
            stockPriceThread.run();
        });*/
        StockPriceThread stockPriceThread = new StockPriceThread("600651", stockPriceService);
        stockPriceThread.run();

    }

    /**
     * 获取公司信息
     */
    @GetMapping(value = "getCompanyInfo")
    public void getCompanyInfo() {
        List<String> codeList = stockBaseService.getList();
        System.out.println(codeList.size());
        codeList.parallelStream().forEach(code -> {
            try {
                CompanyInfoThead companyInfoThead = new CompanyInfoThead(code, companyInfoService);
                companyInfoThead.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取公司发行方信息
     */
    @GetMapping(value = "getCompanyIssueInfo")
    public void getCompanyIssueInfo() {
        List<String> codeList = stockBaseService.getList();
        System.out.println(codeList.size());
        codeList.parallelStream().forEach(code -> {
            try {
                StockIssueInfoThread stockIssueInfoThread = new StockIssueInfoThread(code, stockIssueInfoService);
                stockIssueInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取公司参股公司信息
     */
    @GetMapping(value = "getHoldOrgInfo")
    public void getHoldOrgInfo() {
        List<String> codeList = stockBaseService.getList();
        System.out.println(codeList.size());
        codeList.parallelStream().forEach(code -> {
            try {
                HoldOrgInfoThread holdOrgInfoThread = new HoldOrgInfoThread(code, holdOrgInfoService);
                holdOrgInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取股东人数的数据
     */
    @GetMapping(value = "getStockHolderNumsInfo")
    public void getStockHolderNumsInfo() {
        List<String> codeList = stockBaseService.getList();
        List<String> handledCodeList = stockHolderNumsInfoService.getHandledCodeList();
        codeList.stream().filter(code -> !handledCodeList.contains(code)).forEach(code -> {
            try {
                StockHolderInfoThread stockHolderInfoThread = new StockHolderInfoThread(code, stockHolderNumsInfoService);
                stockHolderInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取流通十大股东
     */
    @GetMapping(value = "getStockFreeHolderInfo")
    public void getStockFreeHolderInfo() {
        List<String> codeList = stockBaseService.getList();
        codeList.forEach(code -> {
            try {
                StockFreeHoldersInfoThread stockFreeHoldersInfoThread = new StockFreeHoldersInfoThread(code, stockFreeHoldersInfoService);
                stockFreeHoldersInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取十大股东
     */
    @GetMapping(value = "getStockFixHolderInfo")
    public void getStockFixHolderInfo() {
        List<String> codeList = stockBaseService.getList();
        codeList.forEach(code -> {
            try {
                StockFixHolerInfoThread stockFreeHoldersInfoThread = new StockFixHolerInfoThread(code, stockFixHolderInfoService);
                stockFreeHoldersInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    /**
     * 获取主力持仓信息
     */
    @GetMapping(value = "getMainHolderInfo")
    public void getMainHolderInfo() {
        List<String> codeList = stockBaseService.getList();
        codeList.remove("600519");
        codeList.forEach(code -> {
            try {
                StockMainHolderInfoThread mainHolderInfoThread = new StockMainHolderInfoThread(code, stockMainHolderInfoService);
                mainHolderInfoThread.run();
            } catch (Exception exception) {
                System.out.println(code);
                System.out.println(exception.getMessage());
            }
        });
    }

    @GetMapping(value = "test")
    public void test() {
        System.out.println(Runtime.getRuntime().availableProcessors());
    }

    @GetMapping(value = "insert")
    public void insert() {
        var list = dfcfServiceApi.getStockList();

        stockBaseService.insertStockBase(list);
    }


}
